<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller\room;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\room\RoomHospitalDeviceLists;
use app\adminapi\logic\room\RoomHospitalDeviceLogic;
use app\adminapi\validate\room\RoomHospitalDeviceValidate;
use app\common\model\Building;
use app\common\model\room\department\RoomDepartment;
use app\common\model\room\department\RoomDepartmentDevice;
use app\common\model\room\RoomHospital;
use app\common\model\room\RoomHospitalDevice;
use app\common\model\room\RoomHospitalDeviceLog;
use app\common\model\user\User;
use app\common\service\ConfigService;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Config;
use think\facade\Db;


/**
 * 全院设备关联控制器
 * Class RoomHospitalDeviceController
 * @package app\adminapi\controller\room
 */
class RoomHospitalDeviceController extends BaseAdminController {


    /**
     * @notes 获取全院设备关联列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function lists() {

//        $spreadsheet = IOFactory::load('hXXXX.xls');
//        $sheet = $spreadsheet->getActiveSheet();
//        $data = [];
//        foreach ($sheet->getRowIterator() as $row) {
//            $rowData = [];
//            $cellIterator = $row->getCellIterator();
//            $cellIterator->setIterateOnlyExistingCells(false);
//            foreach ($cellIterator as $cell) {
//                $rowData[] = $cell->getValue();
//            }
//            $data[] = $rowData;
//        }
//        $c = Building::where("status", 2)->count();
//        if ($c > 0) {
//            var_dump(123123);
//            exit;
//        }
//        $save_all = [];
//        for ($i = 0; $i <= 7; $i++) {
//            $save_all[] = [
//                "cid" => 1,
//                "name" => $i,
//                "status" => 2,
//                "all_lift_fee" => 0,
//                "all_second_pressure_fee" => 0,
//                "apartment_lift_fee" => 0,
//                "apartment_second_pressure_fee" => 0,
//            ];
//        }
//        (new Building())->saveAll($save_all);

//        $c = RoomDepartmentDevice::where("room_master", 1)->count();
//        if ($c > 0) {
//            var_dump(123123);
//            exit;
//        }
//        $ddc = ConfigService::get('room_apartment', 'd_device_coefficient');
//        $ddp = ConfigService::get('room_apartment', 'd_device_price');
//        $room = RoomDepartment::where("status", 1)->select()->toArray();
//        $d_lists = [];
//        foreach ($room as $v) {
//            $d_lists[] = [
//                "cid" => $v['cid'],
//                "bid" => $v['bid'],
//                "rid" => $v['id'],
//                "room_master" => 1,
//                "sort" => $params['sort'] ?? 10000,
//                "create_time" => date("Y-m-d H:i:s"),
//                "update_time" => date("Y-m-d H:i:s"),
//                "device_type" => 2, // 默认电表
//                "coefficient" => $ddc, // 系数
//                "price" => $ddp, // 水电价
//            ];
//        }
//        (new RoomDepartmentDevice())->saveAll($d_lists);
//        var_dump(123123);
//        exit;

        return $this->dataLists(new RoomHospitalDeviceLists());
    }

    /**
     * @notes 添加全院设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function add() {
        $params = (new RoomHospitalDeviceValidate())->post()->goCheck('add');
        $result = RoomHospitalDeviceLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(RoomHospitalDeviceLogic::getError());
    }


    /**
     * @notes 编辑全院设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function edit() {
        $params = (new RoomHospitalDeviceValidate())->post()->goCheck('edit');
        $result = RoomHospitalDeviceLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RoomHospitalDeviceLogic::getError());
    }

    public function setSave() {
        $params = input();
        $result = RoomHospitalDeviceLogic::setSave($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RoomHospitalDeviceLogic::getError());
    }

    /**
     * @notes 删除全院设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function delete() {
        $params = (new RoomHospitalDeviceValidate())->post()->goCheck('delete');
        RoomHospitalDeviceLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取全院设备关联详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function detail() {
        $params = (new RoomHospitalDeviceValidate())->goCheck('detail');
        $result = RoomHospitalDeviceLogic::detail($params);
        return $this->data($result);
    }

    public function copyAdd() {
        $params = input();
        $time = date("Y-m-d H:i:s");

        if ($params['device_type'] == 1) {
            $dc = ConfigService::get('room_apartment', 's_device_coefficient');
            $dp = ConfigService::get('room_apartment', 's_device_price');
        } else {
            $dc = ConfigService::get('room_apartment', 'd_device_coefficient');
            $dp = ConfigService::get('room_apartment', 'd_device_price');
            $params['device_type'] = $params['device_type'] ? $params['device_type'] : 2;
        }
        $room_model = new RoomHospitalDevice();
        $lists = $room_model->field([
            "cid", "bid", "rid", "sort",
            "'" . $params['device_type'] . "' as device_type",
            "'" . $dc . "' as coefficient",
            "'" . $dp . "' as price",
            "'0' as room_master", "'" . $time . "' as create_time", "'" . $time . "' as update_time",
        ])->whereIn("id", $params['id'])->append([
            "copy_add"
        ])->select()->toArray();
        if (!$lists) {
            return $this->fail('查询失败');
        }
        $res = $room_model->strict(false)->insertAll($lists);
        if (!$res) {
            return $this->fail('操作失败');
        }
        return $this->success('新增成功');
    }

}
